package easy.netty.net.core;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author cengy
 */
public class Entity {

    protected IoSession session;
    protected String id;

    protected Map<String, Component> compMap = new HashMap<>(16);

    public Entity() {
        this(null);
    }

    public Entity(IoSession session) {
        this.session = session;
        this.id = UUID.randomUUID().toString().replaceAll("-", "");
    }

    public IoSession getSession() {
        return session;
    }

    public void setSession(IoSession session) {
        this.session = session;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Entity addComp(Component component) {
        if (null != component) {
            compMap.put(component.getClass().getSimpleName(), component);
        }

        return this;
    }

    public Entity removeComp(Component component) {
        if (null != component) {
            compMap.remove(component.getClass().getSimpleName());
        }

        return this;
    }

    public void send(Object message) {
        if (null != session && null != message) {
            session.send(message);
        }
    }

    public void send(Object message,
                     IoSession.SendMessageCallback callback) {
        if (null != session && null != message) {
            session.send(message, callback);
        }
    }
}
